package com.xhrd.framework.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

public class StringUtil {

    /**
     * 判断一个字符串是否为有效字符串
     *
     * @param str
     * @return
     */
    public static boolean isValidString(String str) {
        return (str != null && StringUtils.isNotBlank(str) && StringUtils.isNotEmpty(str));
    }

    /**
     * 重置StringBuilder
     *
     * @param sbf
     */
    public static void resetStrBuf(StringBuilder sbf) {
        sbf.delete(0, sbf.length());
    }

    /**
     * 字符串左侧补0。
     *
     * @param str
     *            待补足字符串
     * @param length
     *            总长度
     */
    public static String leftAppendZero(String str, int length) {
        return StringUtils.leftPad(str, length, '0');
    }

    /**
     * 半角转全角
     *
     * @param input
     * @return
     */
    public static String DBCToSBC(String input) {
        // 半角转全角：
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 32) {
                c[i] = (char) 12288;
                continue;
            }
            if (c[i] < 127)
                c[i] = (char) (c[i] + 65248);
        }
        return new String(c);
    }

    /**
     * 全角转半角
     *
     * @param input
     * @return
     */
    public static String SBCToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    /**
     * 根据浏览器信息编码文件名称
     *
     * @param fileName
     * @param agent
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encodeFileName(String fileName, String agent) {
        String encodedFileName = "";
        try {
            if (null != agent && -1 != agent.indexOf("MSIE")) {
                encodedFileName = URLEncoder.encode(fileName, "UTF-8");
            } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
                encodedFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))) + "?=";
            } else {
                encodedFileName = fileName;
            }
        } catch (UnsupportedEncodingException e) {
            // do noting!
        }
        return encodedFileName;
    }


    /**
     * 将文件名称转换为UUID
     *
     * @param fileName
     * @return
     */
    public static String getNameUUID(String fileType) {
        return UUID.randomUUID().toString().replace("-", "") + "." + getFilePostfix(fileType);
    }

    /**
     * 由时间字符串和四位随机数构成的18-20位数的Long型随机数
     *
     * @return
     */
    public static Long generateLongIdentity() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuilder generate = new StringBuilder(sdf.format(new Date()));
        generate.append(Math.round(Math.random() * 10000));
        return Long.parseLong(generate.toString());
    }

    public static String generateStringIdentity() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuilder generate = new StringBuilder(sdf.format(new Date()));
        generate.append(Math.round(Math.random() * 10000));
        return generate.toString();
    }

    /**
     * 获得文件后缀名
     *
     * @param fileName
     * @return
     */
    public static String getFilePostfix(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
    }
}
